package com.wsoft.vo;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.wsoft.constant.CommonConstant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 当前登录用户缓存对象
 *
 * @author: juiet
 * @date: 2024年09月03日 13:57
 */
@NoArgsConstructor
@Data
@JsonSerialize
@JsonIgnoreProperties(ignoreUnknown = true)
public class LoginUserDetail implements UserDetails,Serializable {
    private static final long serialVersionUID = -1;

    private LoginUserVO loginUserVO;
    private List<ResourceVO> resourceList;
    private Collection<GrantedAuthority> authorities;

    public LoginUserDetail(LoginUserVO loginUserVO,List<ResourceVO> resourceList){
        this.loginUserVO = loginUserVO;
        this.resourceList = resourceList;
        this.authorities = resourceList.stream().map(resource -> new SimpleGrantedAuthority(resource.getId()+":"+resource.getName())).collect(Collectors.toList());
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.loginUserVO.getLoginPw();
    }

    @Override
    public String getUsername() {
        return this.loginUserVO.getLoginName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return false;
    }
}
